package com.aplink.generic.widgets;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TabHost.TabSpec;

import com.aplink.dellcommercial.R;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.BundleConstant;
import com.aplink.generic.constant.TabhostDisplay;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.PopupMenuSpec;
import com.aplink.generic.pojo.controlspec.PopupMenuSpec.PopupMenuItem;
import com.aplink.generic.pojo.controlspec.TabHostSpec;
import com.aplink.generic.pojo.controlspec.TabHostSpec.TabItem;
import com.aplink.generic.util.LogUtil;

import java.util.HashMap;

public class CustomTabhost extends BaseView {
    private FragmentTabHost mFragmentTabHost;
    private LogUtil mLogUtil = new LogUtil( getClass() );
    private HashMap<View, PopupMenu> mPopupMenu;
    private HashMap<View, Integer> mTabsWidget;

    public CustomTabhost( GenericFragment genericFragment,
                          ControlSpec controlSpec, View view ) {
        super( genericFragment, controlSpec, view );
        setFragmentTabHost( (FragmentTabHost) view );
        mFragmentTabHost.setup( getApplicationContext(),
                getChildFragmentManager(), R.id.realtabcontent );
        mPopupMenu = new HashMap<View, PopupMenu>();
        mTabsWidget = new HashMap<View, Integer>();
    }

    @Override
    protected void loadDataToView() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void doIfNotHashPassedValues() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void setEventListener() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void initViewProperties() {
        TabHostSpec tabhostFormat = (TabHostSpec) getControlSpec();
        if ( tabhostFormat.getBackgroundColor() != null ) {
            mFragmentTabHost.setBackgroundResource( tabhostFormat
                    .getBackgroundColorId() );
        }
        for ( int i = 0 ; i < tabhostFormat.getTabItems().size() ; i++ ) {
            final TabItem tabItem = tabhostFormat.getTabItems().get( i );
            try {
                TabSpec tabSpec = mFragmentTabHost.newTabSpec( tabItem.getTag() );
                if ( tabhostFormat.getDisplay().equals( TabhostDisplay.Title ) ) {
                    tabSpec.setIndicator( tabItem.getTitle(), null );
                } else if ( tabhostFormat.getDisplay().equals(
                        TabhostDisplay.Icon ) ) {
                    Drawable drawable = getResources().getDrawable(
                            tabItem.getIconId() );
                    tabSpec.setIndicator( "", drawable );
                } else if ( tabhostFormat.getDisplay().equals(
                        TabhostDisplay.TitleAndIcon ) ) {
                    Drawable drawable = getResources().getDrawable(
                            tabItem.getIconId() );
                    tabSpec.setIndicator( tabItem.getTitle(), drawable );
                }
                if ( tabItem.getClassName() != null ) {
                    Class<?> genericFragment = Class.forName( tabItem
                            .getClassName() );
                    mFragmentTabHost.addTab( tabSpec, genericFragment, null );
                } else {
                    mFragmentTabHost.addTab( tabSpec, Fragment.class, null );
                }
                View anchorView = mFragmentTabHost.getTabWidget().getChildAt( i );
                if ( tabItem.getPopupMenuFormat() != null ) {
                    createPopupMenu( anchorView, tabItem.getPopupMenuFormat(), i );
                }
                mTabsWidget.put( anchorView, i );
                anchorView.setOnClickListener( mShowPopupMenu );
            } catch ( ClassNotFoundException e ) {
                mLogUtil.error( tabItem.getClassName() + " is not found",
                        "setupViewProperties", e );
            }
        }
        mFragmentTabHost.setCurrentTab( tabhostFormat.getStartTab() );
    }

    private View.OnClickListener mShowPopupMenu = new View.OnClickListener() {

        @Override
        public void onClick( View paramView ) {
            PopupMenu popupMenu = mPopupMenu.get( paramView );
            int selectedTab = mTabsWidget.get( paramView );
            ( (FrameLayout) getActivity().findViewById( R.id.realtabcontent ) )
                    .removeAllViews();
            mFragmentTabHost.setCurrentTab( selectedTab );
            if ( popupMenu != null ) {
                popupMenu.show();
            }
        }
    };

    private void createPopupMenu( View anchorView,
                                  PopupMenuSpec popupMenuFormat, int tabIndex ) {
        PopupMenu mypopupmenu = new PopupMenu( getApplicationContext(),
                anchorView );
        for ( PopupMenuItem item : popupMenuFormat.getPopupMenuItems() ) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable( BundleConstant.POPUP_MENU_ITEM, item );
            bundle.putInt( BundleConstant.SELECTED_INDEX, tabIndex );
            intent.putExtras( bundle );
            mypopupmenu.getMenu().add( item.getTitle() ).setIntent( intent );
        }
        mPopupMenu.put( anchorView, mypopupmenu );
        mypopupmenu.setOnMenuItemClickListener( onPopupMenuItemClick );
    }

    private OnMenuItemClickListener onPopupMenuItemClick = new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick( MenuItem item ) {
            Bundle bundle = item.getIntent().getExtras();
            PopupMenuItem popupMenuItem = (PopupMenuItem) bundle
                    .getSerializable( BundleConstant.POPUP_MENU_ITEM );
            int tabId = bundle.getInt( BundleConstant.SELECTED_INDEX );
            mFragmentTabHost.setCurrentTab( tabId );
            try {
                Fragment fragment = (Fragment) Class.forName(
                        popupMenuItem.getClassName() ).newInstance();
                FragmentTransaction transaction = getChildFragmentManager()
                        .beginTransaction();
                transaction.replace( R.id.realtabcontent, fragment );
                transaction.commit();
            } catch ( InstantiationException e ) {
                e.printStackTrace();
            } catch ( IllegalAccessException e ) {
                e.printStackTrace();
            } catch ( ClassNotFoundException e ) {
                e.printStackTrace();
            }
            return true;
        }
    };

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        return null;
    }

    public FragmentTabHost getFragmentTabHost() {
        return mFragmentTabHost;
    }

    public void setFragmentTabHost( FragmentTabHost fragmentTabHost ) {
        mFragmentTabHost = fragmentTabHost;
    }
}
